package Test;

public class Test5 {
    public static int lengthOfLastWord(String s){
        String[] s1 = s.split(" ");
        return s1[s1.length-1].length();
    }

    public static int lengthOfLastWord2(String s){
        int index = s.length() - 1;
        while(s.charAt(index) == ' '){
            index--;
        }
        for(int i = index; i >= 0; i--){
            if(s.charAt(i) == ' '){
                return index - i;
            }
        }
        return index+1;
    }

    public static void main(String[] args) {
        String s = "   joyboy    ";
        System.out.println(lengthOfLastWord2(s));
    }
}
